import {Application} from "pinus";
import controlDao, { ControlDao } from "../dao/controlDao";

export default function (app: Application) {
    return new ControlService(app)
}

export class ControlService {

    private _controlDao: ControlDao

    public constructor(private app: Application) {
        this._controlDao = controlDao(app)
    }

    public async getRobotConfig(): Promise<{multipleRange:{minMultiple: number, maxMultiple: number, chance: number, base: number}[], betRange:{betBase: number,minMultiple: number,maxMultiple: number,chance: number, base: number}[], bettingOdds: number, robotNumber: number}> {
        let config = await this._controlDao.getRobotConfig()
        let configResults = config.results
        if(configResults.length !== 1) {
            return null
        }
        let minMultiple = 1.1
        const multipleRange: string[] = configResults[0].multipleRange.split(";")
        const betRange: string[] = configResults[0].betRange.split(";")
        let multipleResult = []
        let betResult = []
        multipleRange.forEach(e => {
            const range = e.split(',')
            if (range.length !== 2){
                return
            }
            multipleResult.push({
                minMultiple: minMultiple,
                maxMultiple: parseInt(range[0]),
                chance: parseInt(range[1]),
                base: 1
            })
            minMultiple = parseInt(range[0])
        })
        betRange.forEach(e => {
            const range = e.split(',')
            if(range.length !== 3){
                return
            }
            betResult.push({
                base: parseInt(range[0]),
                minMultiple: 1,
                maxMultiple: parseInt(range[1]),
                chance: parseInt(range[2])
            })
        })
        return {
            multipleRange: multipleResult,
            betRange: betResult,
            bettingOdds: configResults[0].bettingOdds,
            robotNumber: configResults[0].robotNumber
        }
    }

    public async getGameConfig(): Promise<{minBet: number, maxBet: number, minMultiple: number, maxMultiple: number}> {
        let config = await this._controlDao.getGameConfig()
        let configResults = config.results
        if(configResults.length !== 1) {
            return null
        }
        return {
            minBet: configResults[0].minBet,
            maxBet: configResults[0].maxBet,
            minMultiple: configResults[0].minMultiple,
            maxMultiple: configResults[0].maxMultiple
        }
    }

    public async getControlConfig(): Promise<{id: number, reserve: number, progress: number, rebate: number}> {
        let config = await this._controlDao.getControlConfig()
        let configResults = config.results
        if(configResults.length !== 1) {
            return null
        }
        return {
            id: configResults[0].id,
            reserve: configResults[0].reserve,
            progress: configResults[0].progress,
            rebate: configResults[0].rebate
        }
    }

    public async saveProgress(progress: number, id: number) {
        return await this._controlDao.saveProgress(progress, id)
    }

    public async getGameControlSetting() {
        let config = await this._controlDao.getGameControlSetting()
        let configResults = config.results
        if(configResults.length !== 1) {
            return null
        }
        return {
            gameStatus: configResults[0].gameStatus,
            gameMaxMultiple: configResults[0].gameMaxMultiple,
        }
    }
}
